//
//  MyLabelUpView.m
//  FD
//
//  Created by xiaofang wang on 2018/12/30.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyLabelUpView.h"

#import "DCCollectionHeaderLayout.h"
#import "MyLabelModel.h"
#import "XJFeatureItem.h"
#import "XJFeatureSectionHeaderView.h"

static NSString *const XJFeatureItemReuseId = @"XJFeatureItemReuseId";

@interface MyLabelUpView()<UICollectionViewDelegate,
UICollectionViewDataSource,
HorizontalCollectionLayoutDelegate>

/**1.10px灰色线*/
@property (nonatomic, strong) UILabel *lineView;
/**2.collectionView*/
@property (nonatomic, strong) UICollectionView *collectionView;
/**3.我的标签Lb*/
@property (nonatomic, strong) UILabel *myTagLabel;

@end

@implementation MyLabelUpView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
//        [self fakeData];
    }
    return self;
}


- (void)setupUI {
    [self addSubview:self.lineView];

    [self addSubview:self.myTagLabel]; //我的标签:
    
    [self addSubview:self.collectionView];
}


#pragma mark - UICollectionViewDelegate,
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.myTagArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XJFeatureItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:XJFeatureItemReuseId forIndexPath:indexPath];
    MyLabelModel *itemM = self.myTagArr[indexPath.row];
    item.itemModel = itemM;

    return item;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //取出model
    MyLabelModel *model = self.myTagArr[indexPath.row];
    
    //回调出去
    if (self.upViewClickItemBlock) {
        self.upViewClickItemBlock(model);
    }
}

#pragma mark - HorizontalCollectionLayoutDelegate
// item宽度自适应
- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath {
    MyLabelModel *model = self.myTagArr[indexPath.row];
//    FDLog(@"**************:%@",model);
    return model.name;
}

//UICollectionViewDataSource,
//UICollectionViewDelegateFlowLayout

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //灰色的线
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(10);
    }];
    
    //我的标签
    [self.myTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.lineView.mas_bottom);
        make.height.mas_equalTo(30);
        make.width.mas_greaterThanOrEqualTo(100); //2019-2-23宽度自适应
    }];
    
    //collectionView
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myTagLabel.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}


- (void)fakeData {
    NSArray *tempUPArr = @[@{@"name":@"昵称"},
                           @{@"name":@"手擀面"},
                           @{@"name":@"饺子"},
                           @{@"name":@"甜食"},
                           @{@"name":@"爱吃青菜"},
                           @{@"name":@"五个字的"}].mutableCopy;

    NSMutableArray *arrM = [NSMutableArray array];
    
    //转Model后传过去
    for (NSDictionary *dic1 in tempUPArr) {
        MyLabelModel *model = [MyLabelModel mj_objectWithKeyValues:dic1];
        [arrM addObject:model];
    }
    self.myTagArr = [arrM copy];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

#pragma mark - 接口
- (void)upViewLoadData {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.collectionView reloadData];
    });
}


#pragma mark - getter
- (UILabel *)lineView {
    if (!_lineView) {
        _lineView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        _lineView.backgroundColor = kMainLightGrayColor;
    }
    return _lineView;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        DCCollectionHeaderLayout *layout = [DCCollectionHeaderLayout new];
        layout.lineSpacing = 8;
        layout.interitemSpacing = 20;
        layout.delegate = self;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        //代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        //注册cell & SectionHeaderView
        [_collectionView registerClass:[XJFeatureItem class] forCellWithReuseIdentifier:XJFeatureItemReuseId];
    }
    return _collectionView;
}

- (UILabel *)myTagLabel {
    if (!_myTagLabel) {
        _myTagLabel = [[UILabel alloc] init];
        _myTagLabel.text = NSLocalizedString(MyTags, @"我的标签:");
    }
    return _myTagLabel;
}
@end
